<?php

namespace App\Miya\Middlewares;

use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class SourceCode
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $cacheKey = 'source_file_hash_chech';
        if (!Cache::has($cacheKey)) {
            $source_hash = md5(file_get_contents(app_path('Miya/Providers/Miya.php')));
            $hit_hash = trim(Storage::get('source.h'));
            $result = ($source_hash == $hit_hash);
            if ($result) Cache::add($cacheKey, true, 60 * 60 * 24);
        } else {
            $result = Cache::get($cacheKey);
        }

        if ($result) {
            return $next($request);
        } else {
            fuckYou(500, 'miya.source_file_falsify', true);
        }
    }
}
